package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.BuyDetailListAdapter;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.BuyappSubOriginal;
import com.isunland.managesystem.entity.DataStatus;
import com.isunland.managesystem.entity.rBuyappMain;
import com.isunland.managesystem.entity.rBuyappSub;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/6/15 14:09
 */
public class BuyDetailListFragment extends BaseListFragment {

  private static String EXTRA_CONTENT =
      BuyDetailListFragment.class.getSimpleName() + "EXTRA_CONTENT";
  private rBuyappMain mContent;
  private ArrayList<rBuyappSub> mList;
  private int REQUEST_DETAIL = 0;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    mContent = (rBuyappMain) getArguments().getSerializable(EXTRA_CONTENT);
    mList = new ArrayList<>();
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    if (DataStatus.NEW.equalsIgnoreCase(mContent.getBuyappStatusName())
        || DataStatus.ABORT.equalsIgnoreCase(mContent.getBuyappStatusName())) {
      inflater.inflate(R.menu.menu_add, menu);
    }
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        getActivity().finish();
        return true;

      case R.id.menu_item_add:
        Intent intent = new Intent(getActivity(), BuyDetailActivity.class);
        intent.putExtra(BuyDetailFragment.EXTRA_MAIN, mContent);//主表
        startActivityForResult(intent, REQUEST_DETAIL);
        return true;

      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Override public String getUrl() {
    return ApiConst.URL_BUY_DETAIL_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    ParamsNotEmpty params = new ParamsNotEmpty();
    params.put("mainid", mContent.getId());
    return params.getParams();
  }

  @Override public void resolveResponse(String json) {
    BuyappSubOriginal original = new Gson().fromJson(json, BuyappSubOriginal.class);
    if (original != null && Base.SUCCESS == original.getResult()) {
      mList.clear();
      mList.addAll(original.getRows());
      BuyDetailListAdapter mAdapter = new BuyDetailListAdapter(getActivity(), mList);
      setListAdapter(mAdapter);
    } else {
      ToastUtil.show("获取列表失败");
    }
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {

    rBuyappSub content = mList.get(position - 1);
    Intent intent = new Intent(getActivity(), BuyDetailActivity.class);
    intent.putExtra(BuyDetailFragment.EXTRA_CONTENT, content);
    intent.putExtra(BuyDetailFragment.EXTRA_MAIN, mContent);//主表
    startActivityForResult(intent, REQUEST_DETAIL);
    super.onListItemClick(l, v, position, id);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) return;
    volleyPost();
    super.onActivityResult(requestCode, resultCode, data);
  }

  public static BuyDetailListFragment newInstance(rBuyappMain content) {
    Bundle args = new Bundle();
    args.putSerializable(EXTRA_CONTENT, content);
    BuyDetailListFragment fragment = new BuyDetailListFragment();
    fragment.setArguments(args);
    return fragment;
  }
}
